/*****************************************************************************************
 * FILE NAME: srv_variable                       COPYRIGHT (c) cos 2024
 * REVISION:  1.0.0                                     All Rights Reserved
 * DESCRIPTION:
 * =======================================================================================
 * UPDATE HISTORY：
 * REV      AUTHOR      DATE
 * v1.0.0   Tuxin       2024.10.14
 * DESCRIPTION OF CHANGE:
 ****************************************************************************************/
#ifndef __SRV_VARIABLE_H
#define __SRV_VARIABLE_H

//----------------------------------------------------------------------------------------
// @Project Includes
#include "typedefs.h"

//----------------------------------------------------------------------------------------
// @Global Macro Define in the module
#define	MAX_BLE_MAC_LEN            	 (13u)	

//----------------------------------------------------------------------------------------
// @Global Type defines in the module
typedef enum en_wakeup_source
{
    WAKEUP_NULL    		= 0,
    WAKEUP_LOAD 			= 1,
    WAKEUP_CHARGE 		= 2,
    WAKEUP_AFE				= 3,
    WAKEUP_SW					= 4,
    WAKEUP_CAN				= 5,  
    WAKEUP_RS485			= 6,  
    WAKEUP_COMMUN			= 7,   
    WAKEUP_RTC				= 8,
      
}en_wakeup_source_t;

typedef union un_software_protectstatus
{
    struct
    {
        uint32_t  ov       			:1;/* 电芯过呀 */
        uint32_t  uv       			:1;/* 电芯欠压*/			
        uint32_t  occ       		:1;/* 充电过流*/
        uint32_t  ocd       		:1;/* 放电过流*/

        uint32_t  sc       			:1;/* 短路*/
        uint32_t  otc       		:1;/* 充电高温*/
        uint32_t  utc       		:1;/* 充电低温*/
        uint32_t  otd      			:1;/* 放电高温*/

        uint32_t  utd       		:1;/* 放电低温 */
        uint32_t  otcdm  				:1;/* MOS高温*/ 
        uint32_t  utcdm 				:1;/* MOS低温*/  
        uint32_t  otamb      		:1;/* 环境高温*/

        uint32_t  utamb  	    	:1;/* 环境低温*/
        uint32_t  diffvol  	    :1;/* 压差*/			
        uint32_t  tov        		:1;/* 总压过压*/
        uint32_t  tuv        		:1;/* 总压欠压*/

        uint32_t  difftemp  	  :1;/* 温差*/	
        uint32_t  soclow  	    :1;/* SOC低*/	
        uint32_t  full  	    	:1;/* 充满*/					
        uint32_t  line  	    	:1;/* 断线，绝缘*/
				
        uint32_t  back  	    	:12;/* 备份*/			
			
    }bit;
    uint32_t  dword;
}un_software_protectstatus_t;/*告警标志位联合体*/


typedef union un_modle_workstate
{
    struct
    {
        uint32_t  bms       		:2;/* BMS模块 */
        uint32_t  g4       			:2;/* 4G模块*/			
        uint32_t  bt       			:2;/* 蓝牙模块*/
        uint32_t  gps      			:2;/* GPS模块*/

        uint32_t  back       		:4;/* 预留*/
        uint32_t  charge       	:1;/* 充电器连接状态*/
        uint32_t  net       		:1;/* 后台连接状态*/
        uint32_t  light      		:1;/* 光感*/
        uint32_t  sim      			:1;/* sim*/
			
				uint32_t  back1       	:16;/* 预留*/
			
    }bit;
    uint32_t  dword;
		
}un_modle_workstate_t;/*告警标志位联合体*/

typedef enum en_ota_state
{
    OTA_STATE_IDLE 		= 0,/*空闲*/
    OTA_STATE_START		= 1,/*启动升级*/
    OTA_STATE_SUCCESS  	= 2,/*升级成功*/	
    OTA_STATE_ERROR 	= 3,/*升级失败*/
    OTA_STATE_VERERROR 	= 4,/*无需升级*/
    OTA_STATE_MAX 		= 5,/**/	
}en_ota_state_t;

#pragma pack(1)
typedef struct stc_upg
{
    uint32_t u32fwSize;/* 需更新固件的文件长度,单位字节*/	
    uint16_t u16fwCrc16;/* 需更新固件的crc校验值 */
    uint16_t u16fwVer;/* 需更新固件的版本号 */
		en_ota_state_t state;	/* 升级状态*/
    uint8_t  u8timeout;/* 升级等待时间*/
    uint8_t  u8ecu;/*ECU类型 */
    uint32_t u32offset;/*升级偏移基地址*/
    uint16_t u16len;/* 申请数据长度*/
    bool_t bsendRequest;/* 发送请求*/
    int32_t i32progress;/* 升级批次*/
}stc_upg_t;

typedef struct stc_upginfo
{
    uint16_t u16flag;/* 固件升级标志0x5aa5则启动升级 */
    uint8_t  u8ecu;/*ECU类型 */
    uint16_t u16fwVer;/* 需更新固件的版本号 */
    uint32_t u32fwSize;/* 需更新固件的文件长度,单位字节*/	
    uint16_t u16fwCrc16;/* 需更新固件的crc校验值 */
}stc_upginfo_t;
#pragma pack()
#define UPDATE_TAG_SIZE	sizeof(stc_upginfo_t)/*13*/


typedef struct stc_Variable
{
	  uint8_t u8bleMac[MAX_BLE_MAC_LEN];/*bt_mac */
	
		bool_t bpvt;/*pvt*/	
		bool_t bcmdSleep;	
	  bool_t bcmdPowerDown;
		uint32_t u32cmdBal;
	
		bool_t bheatFlag;
		bool_t bheatCmd;
    uint16_t  u16canError;/*CAN故障*/	
	
    uint32_t u32fullChgCap;/* 电池包满充容量,单位mAH */			
    uint8_t  u8batteryType;/* 电池类型 */
    uint8_t  u8cellS;/* 电池包串联电芯数 */
	
	  un_modle_workstate_t    unmodleWorkState;/*模块工作状态*/
	  uint32_t u32runTimes;/*the sys run times  1sec*/
	  uint8_t u8hwAddress;/*hw commu address*/
	
    bool_t binitFinishFlg;/* init finish success flag*/
    bool_t bsleepFlag;/*sleep flag*/
    bool_t bwakeupFlag;/*wake up flag*/
	
	
		bool_t bchgMosCmdCtl;/*chg ctl*/	
		bool_t bdsgMosCmdCtl;/*dsg ctl*/	
		bool_t bchgMosCtl;/*chg ctl*/	
		bool_t bdsgMosCtl;/*dsg ctl*/
		bool_t bpchgMosCtl;/*pchg ctl*/	
		bool_t bpdsgMosCtl;/*pdsg ctl*/	
	
		bool_t bchgAfeMosCtl;/*chg ctl*/	
		bool_t bdsgAfeMosCtl;/*dsg ctl*/
		bool_t bpchgAfeMosCtl;/*pchg ctl*/	
		bool_t bpdsgAfeMosCtl;/*pdsg ctl*/		
	
	  uint32_t u32afeRefRs;
		bool_t bafeInitFlag;
	
		bool_t bafeReadOK;
	
		bool_t btimeCraetFlag;/*Timer creation flag*/
	  volatile en_wakeup_source_t enWakeupSource; /*wakeup reason*/
    un_software_protectstatus_t unProtRank1;
    un_software_protectstatus_t unProtRank2;
    un_software_protectstatus_t unProtRank3;		
		
		uint16_t u16parmWriteWaitTime;/*写参数时间*/
		bool_t bparmWriteFlag;/*写参数标志*/
		
		uint32_t u32sleepTime ;
		uint32_t u32sleepEnterTime ;
    uint32_t u32sleepExitTime ;
		
		uint16_t u16sleepWaitTime;/*休眠等待时间*/
		bool_t bbleConnectFlag;

		uint32_t u32totalSumVolt;/*Individual accumulated voltage*/
		uint32_t u32totalPackVolt;/*Total voltage*/	
		uint16_t u16cellVolt[BMS_MAX_CELLNUM];
		uint16_t u16voltMax;
		uint8_t u8voltMaxNum;
		uint16_t u16voltMin;
		uint8_t u8voltMinNum;
		uint16_t u16voltAvg;
		uint16_t u16voltDiff;
		bool_t bvoltValidFlg;
		
		uint32_t u32balStat;/* 均衡状态*/
		
		int8_t i8cellTemp[BMS_MAX_TEMPNUM];
		int8_t i8tempMax;
		int8_t i8tempMin;
		int8_t i8tempAvg;
		uint8_t u8tempDel;
		uint8_t u8maxTmprPos;
		uint8_t u8minTmprPos;
		bool_t btmprValidFlg;

		int8_t i8chgMosTemp;
		int8_t i8disMosTemp;
		int8_t i8mosTemp;
		int8_t i8ambTemp;		
		uint16_t u16vbatVol;
		int16_t i16upVol;

		int32_t i32avgCurrent;
		
		stc_upginfo_t stcUpgInfo;/*存FLASH用*/
    stc_upg_t stcUpg;/*数据交换用用*/		
	
}stc_Variable_t;

//----------------------------------------------------------------------------------------
// @Export global Variables declare in the module
extern stc_Variable_t g_stcVar;/*Global variable structure*/

//----------------------------------------------------------------------------------------
// @Export declaration of functions in the module
void srv_var_init(void);

#endif

// =======================================================================================
// End of file.
// =======================================================================================
